/**
 * Created by ZFL on 2017/11/8.
 */


function appInit(){
    PageInit();
}
function PageInit(){
    if($.getPara('storeId')){
        reEdit();

    }
    classify();     //选择分类
    getAddress();   //选择省市区
}
var vm=new Vue({
    el:'#storeRecruit',
    data:{
        parentCategoryList:[],    //二级分类
        childCategoryList:[],   //三级分类
        categoryParent:'',  //二级分类名称
        category:'',   //三级分类名称
        addressInf:'',         //要编辑的地址
        addressInfo_list:[],
        citysList:[],                 //城市列表
        areasList:[],                 //地区列表
        proSelected:-1,               //选中省时
        citySelected:-1,              //选中市时
        areaSelected:-1,              //选中地区时
        proName:'请选择',              //省名
        cityName:'请选择',             //市名
        areaName:'请选择',             //地区名
        selected:1,
        storeName:'',                 //店铺名称
        tel:'',                        //店铺电话
        shopKeeper:'',                 //店老板手机号
        categoryParentId:'',           //二级分类
        categoryId:'',                 //三级分类
        businessLicenceName:'',        //营业执照名称
        businessLicence:'',            //营业执照
        idCardHand:'',                  //手持身份证
        province:'',                   //省
        city:'',                       //市
        area:'',                       //区
        address:'',                    //详细地址
        settlementAccount:'',           //结算账户
        settlementBank:'',              //开户支行
        settlementMan:'',               //结算人
        agentUserNum:'',                //介绍人
        shopHours:'',                   //营业时间
        saleBeginTime:'',               //营业开始时间
        saleEndTime:'',                 //营业结束时间
        discount:'',                    //线上买单优惠比例
        discountRate:'',                //线下买单优惠比例
        navigateImage:[],               //门店形象图
        logoPath:'',                    //门店logo
        serviceImage:[],                //门店服务图
    },
    methods:{
        isEdit:function(){
            if(vm.isOnline==1){
                YDUI.dialog.toast('如需更改账号，请联系客服',1000)
            }
        }
    }
})
$().ready(function(e){
    $("#saleBeginTime").hunterTimePicker();
    $("#saleEndTime").hunterTimePicker();
})

var storeId='';   //店铺Id


//失去焦点事件
$('input').blur(function(e){
    console.log($(this).val);
    if($(this).val()==''){
        e.preventDefault();
        var placeholder=$(this).attr('placeholder')
        YDUI.dialog.toast(placeholder,1000)
    }
})


//请选择分类
$('.classify .right div').click(function(e){
    e.preventDefault();
    $(this).siblings('.dropList').slideToggle();
})


//选择二级分类
function SelparentCategory(item){
    $('.erji').siblings('.dropList').slideToggle()
    vm.childCategoryList=item.childCategoryList;
    vm.categoryParent=item.parentCategoryName;
    vm.categoryParentId=item.parentCategoryId;
}


//选择三级分类
function SelchildCategory(item){
    $('.sanji').siblings('.dropList').slideToggle()
    vm.category=item.childCategoryName;
    vm.categoryId=item.childCategoryId;
}

/*************************地址信息*********************/
$('#address').on('click', function (event){
    event.stopPropagation();
    $('.destination').slideToggle();
    showBg();
});


var address={};
var provances=[] ;    //省份的集合
var citys=[];         //城市的集合
function getAddress(){
    $.ajaxHandler({
        model: function () {
            var obj={};
            return obj;
        },
        visibleTime: 0,
        dataType: "json",
        visible:'hide',
        api: 'yg_baijiabapp/userAddress/getProvinceCityArea',
        success:function(res){
            if(res.code==10000){
                var data=res.data;
                vm.addressInfo_list=res.data;
                data.forEach(function(value,index){
                    provances.push(value.proName)

                })
            }
        }
    })
}
function classify(){
    $.ajaxHandler({
        model:function(){

        },
        visibleTime: 0,
        dataType: "json",
        approve: true,
//       apiUrl:$.appApi(),
        api: 'yg_baijiaappgoods/category/listStoreCategory',
        success:function(res){
            if(res.code==10000){
                var data=res.data;
                vm.parentCategoryList=data;
            }
        }

    })
}


//点击省   得到所在省的所有城市
function getCitys(c,index){
    vm.citysList= c.citys;
    vm.proSelected=index;
    vm.citySelected=-1;
    vm.proName= c.proName;
    vm.cityName='请选择';
    vm.areaName='请选择';
    vm.selected=2;
}
//得到所在城市的所有区县
function getAreas(a,index){
    vm.areasList=a.areas;
    vm.cityName= a.cityName;
    vm.areaName='请选择';
    vm.citySelected=index;
    vm.areaSelected=-1;
    vm.selected=3;
    $('.destination').css({
        overflow:'visible'
    })
    $('.destination ').animate({
        left:-bw*0.5,
    })

}
//关闭地址联动以及遮罩
function closeDestination(s,index){
    vm.areaSelected=index;
    $('.destination').slideToggle()
    vm.areaName= s.areaName;
    vm.province=vm.proName;
    vm.city=vm.cityName;
    vm.area=vm.areaName;
    vm.addressInf = vm.proName + ' ' + vm.cityName + ' ' + vm.areaName +' ';
    $('#my_inf').show();
    closeBg();
    $('.destination ').animate({
        left:0,
        bottom:0
    })
}

function getSelected(sel){
    vm.selected=sel;
    if(sel==1||sel==2){
        $('.destination ').animate({
            left:0,

        })
    }else if(sel==3){
        $('.destination').animate({
            left:-bw*0.5,

        })
    }else{


    }
}
//获得屏幕的宽高
var bh = $("body").height();
var bw = $("body").width();
//显示灰色 jQuery 遮罩层
function showBg() {
    $("#fullbg").css({
        height:bh,
        width:bw,
        display:"block",
    });
    $('.destinatioWrap').css({
        width:bw*1.5,
    })
    $('.destination .left').css({
        width:bw*0.5
    })
    $('body').css("overflow","hidden");

}
//关闭灰色 jQuery 遮罩
function closeBg(){
    $("#fullbg,#dialog").hide();
    $('body').css("overflow","visible")
}
var storeInfo={};

function uploadImg(n){
    storageInfo();
    switch(n){
        case 0:window.location.href='uploadID.html';   //上传身份证
            break;
        case 1:window.location.href='uploadLicence.html';   //上传营业执照
            break;
        case 2:window.location.href='uploadStFigure.html';   //上传门店形象图
            break;
        case 3:window.location.href='uploadLogo.html';   //上传门店logo
            break;
        case 4:window.location.href='uploadSeImg.html';   //上传商品服务图
            break;
    }
}

//银行卡信息不能修改    点击修改弹出提示信息
$('.balanceInfo div').click(function(){
    YDUI.dialog.toast('如需更改账号,请请联系客服',1000)
})


//重新编辑
function reEdit(){
    $.ajaxHandler({
        model:function(){
            var obj={};
            obj.storeId= $.getPara('storeId');
            return obj;
        },
        visibleTime: 0,
        dataType: "json",
        approve:true,
        api: 'yg_baijiabapp/store/getEditStoreInfo',
        success:function(res){
            if(res.code==10000){
                var data=res.data;
                showData(data)

            }
        }
    })
}

var idCardInfo={};   //存放身份证信息
var licenceInfo={};    //存放营业执照信息
var navigateImage=[];    //存放门店形象图图片
var logoPath='';    //门店logo图
var serviceImage=[];    //存放商品服务图图片
var status= $.getPara('status');    //店铺状态
function showData(data){
    vm.storeName=data.storeName;                //店铺名称
    vm.tel=data.tel;
    vm.shopKeeper=data.shopKeeper;                //店老板手机号
    vm.categoryParentId=data.categoryParentId;           //二级分类
    vm.categoryParent=data.categoryParent;            //二级分类名称
    vm.categoryId=data.categoryId;                //三级分类
    vm.category=data.category;                   //三级分类名称

    //$.setStorage('idCardInfo',idCardInfo);
    licenceInfo.businessLicenceName=data.businessLicenceName;       //营业执照名称
    licenceInfo.businessLicence=data.businessLicence;           //营业执照
    licenceInfo.idCardHand=data.idCardHand;                    //手持身份证
    vm.province=data.province;                   //省
    vm.city=data.city;                       //市
    vm.area=data.area;                      //区
    vm.address=data.address;                  //详细地址
    vm.settlementAccount=data.settlementAccount;          //结算账户
    vm.settlementBank=data.settlementBank;              //开户支行
    vm.settlementMan=data.settlementMan;              //结算人
    vm.agentUserNum=data.agentUserNum;
    //介绍人
    $('#saleBeginTime').val(data.saleBeginTime);
    $('#saleEndTime').val(data.saleEndTime);
    vm.discount=data.discount;                   //线上买单优惠比例
    vm.discountRate=data.discountRate;                //线下买单优惠比例
    navigateImage=data.navigateImage;               //门店形象图

    logoPath=data.logoPath;                   //门店logo

    serviceImage=data.serviceImage;           //商品服务图

    vm.addressInf=data.province+' '+data.city+' '+data.area;   //省市区

    //  若检测到没有本地存储则设置    若检测到则不设置   以防和上传身份证页面   上传营业执照页面  等冲突
    if(status!=4){
        if(!$.getStorage('idCardInfo')){
            $.setStorage('idCardInfo',idCardInfo);
        }

        if(!$.getStorage('licenceInfo')){
            $.setStorage('licenceInfo',licenceInfo);
        }
        if(!$.getStorage('navigateImage')){
            $.setStorage('navigateImage',navigateImage);
        }
        if(!$.getStorage('logoPath')){
            $.setStorage('logoPath',logoPath);
        }
        if(!$.getStorage('serviceImage')){
            $.setStorage('serviceImage',serviceImage);
        }

    }

    if($.getStorage('storeInfo')){

        vm.storeName=$.parseJSON($.getStorage('storeInfo')).storeName;
        vm.tel=$.parseJSON($.getStorage('storeInfo')).tel;
        vm.categoryParent=$.parseJSON($.getStorage('storeInfo')).categoryParent;
        vm.category=$.parseJSON($.getStorage('storeInfo')).category;
        vm.province=$.parseJSON($.getStorage('storeInfo')).province;                   //省
        vm.city=$.parseJSON($.getStorage('storeInfo')).city;                       //市
        vm.area=$.parseJSON($.getStorage('storeInfo')).area;                      //区
        vm.address=$.parseJSON($.getStorage('storeInfo')).address;                  //详细地址

        vm.addressInf =vm.province + ' ' + vm.city+ ' ' + vm.area +' ';

        vm.settlementAccount=$.parseJSON($.getStorage('storeInfo')).settlementAccount;
        vm.settlementBank=$.parseJSON($.getStorage('storeInfo')).settlementBank;
        vm.settlementMan=$.parseJSON($.getStorage('storeInfo')).settlementMan;

        vm.saleBeginTime=$.parseJSON($.getStorage('storeInfo')).saleBeginTime;
        vm.saleEndTime=$.parseJSON($.getStorage('storeInfo')).saleEndTime;

        $('#saleBeginTime').val($.parseJSON($.getStorage('storeInfo')).saleBeginTime);
        $('#saleEndTime').val($.parseJSON($.getStorage('storeInfo')).saleEndTime);

        vm.discount=$.parseJSON($.getStorage('storeInfo')).discount;
        vm.discountRate=$.parseJSON($.getStorage('storeInfo')).discountRate;


    }

    isUpload();


}

//判断身份证  营业执照  门店形象图   门店logo   商品服务图上传后的ui展示
function isUpload(){
    if($.getStorage('idCardInfo')){
        $('.uploadId .prompt').html('已上传法人身份证')
    }

    if($.getStorage('licenceInfo')){
        $('.uploadLicence .prompt').html('已上传营业执照')
    }
    if($.getStorage('navigateImage')){
        $('.uploadFigure div').css({
            border: '1px solid #fd4568'
        })
    }

    if($.getStorage('logoPath')){
        $('.uploadLogo div').css({
            border:'1px solid #fd4568'
        })
    }
    if($.getStorage('serviceImage')){
        $('.uploadServer div').css({
            border:'1px solid #fd4568'
        })
    }
}
var userId= $.getPara('userId');




//存储店铺信息
function  storageInfo(){
    storeInfo.storeName=vm.storeName;
    storeInfo.tel=vm.tel;

    storeInfo.categoryParent=vm.categoryParent;
    storeInfo.category=vm.category;

    storeInfo.province=vm.province;
    storeInfo.city=vm.city;
    storeInfo.area=vm.area;
    storeInfo.address=vm.address;

    storeInfo.settlementAccount=vm.settlementAccount;
    storeInfo.settlementBank=vm.settlementBank;
    storeInfo.settlementMan=vm.settlementMan;

    storeInfo.saleBeginTime=$('#saleBeginTime').val();
    storeInfo.saleEndTime=$('#saleEndTime').val();
    console.log($('#saleBeginTime').val())
    storeInfo.discount=vm.discount;
    storeInfo.discountRate=vm.discountRate;

    $.setStorage('storeInfo',storeInfo)


}
//点击提交按钮
$('.submitBtn').click(function(e){
    e.preventDefault();

    if($("input#storeName").val()!=''&&$("input#tel").val()!=''&&$("input#storeName").val()!=''&&$("input#categoryParent").val()!=''&&$("input#category").val()!=''&&$("input#addressInf").val()!=''&&$("input#detailAddress").val()!=''&&$("input#settlementAccount").val()!=''&&$("input#settlementBank").val()!=''&&$("input#settlementMan").val()!=''&&$('input#agentUserNum').val()!=''&&$('input#saleBeginTime').val()!=''&&$('input#saleEndTime').val()!=''&&$('input#discount').val()!=''&&$('input#discountRate').val()!=''&&$.getStorage('licenceInfo')&&$.getStorage('navigateImage')&&$.getStorage('navigateImage')!=[]&&$.getStorage('logoPath')&&JSON.parse($.getStorage('serviceImage'))&&JSON.parse($.getStorage('serviceImage'))!=[]){
        if($('input#discount').val()>90||$('input#discount').val()<0||$('input#discountRate').val()<0||$('input#discountRate').val()>100){
            YDUI.dialog.toast('请按要求填写优惠比例',1000)
        }else{
            submitEditData()
        }

    }else{
        YDUI.dialog.toast('请将资料填写完整在提交',1000)
    }
})
//重新编辑后提交信息
function submitEditData(){
    $.ajaxHandler({
        model:function(){
            var obj={};
            obj.storeName=vm.storeName;                //店铺名称
            obj.tel=vm.tel;                      //店铺电话
            obj.shopKeeper=vm.shopKeeper;                //店老板手机号
            obj.categoryParentId=vm.categoryParentId;           //二级分类
            obj.categoryId=vm.categoryId;                //三级分类
            obj.businessLicenceName=$.parseJSON($.getStorage('licenceInfo')).businessLicenceName;       //营业执照名称
            obj.businessLicence=$.parseJSON($.getStorage('licenceInfo')).businessLicence;           //营业执照
            obj.idCardHand=$.parseJSON($.getStorage('licenceInfo')).idCardHand;                    //手持身份证
            obj.province=vm.province;                   //省
            obj.city=vm.city;                       //市
            obj.area=vm.area;                      //区
            obj.address=vm.address;                    //详细地址
            obj.settlementAccount=vm.settlementAccount;          //结算账户
            obj.settlementBank=vm.settlementBank;              //开户支行
            obj.settlementMan=vm.settlementMan;              //结算人
            obj.agentUserNum=vm.agentUserNum;               //介绍人
            obj.saleBeginTime=$('#saleBeginTime').val();               //营业开始时间
            obj.saleEndTime=$('#saleEndTime').val();               //营业结束时间
            obj.discount=vm.discount;                   //线上买单优惠比例
            obj.discountRate=vm.discountRate;                //线下买单优惠比例
            obj.navigateImage=$.parseJSON($.getStorage('navigateImage'));               //门店形象图
            obj.logoPath=$.parseJSON($.getStorage('logoPath'));                //门店logo
            obj.serviceImage=$.parseJSON($.getStorage('serviceImage'));               //门店服务图
            obj.storeId= $.getPara('storeId');
            console.log(obj)
            return obj;
        },
        visibleTime: 0,
        dataType: "json",
        approve: true,
        api: 'yg_baijiabapp/store/updateEditStoreInfo',
        success:function(res){
            if(res.code=10000){
                YDUI.dialog.toast(res.message,'success',1000)
                //提交成功后   删除本地存储信息
                $.delStorage('idCardInfo');
                $.delStorage('licenceInfo');
                $.delStorage('navigateImage');
                $.delStorage('logoPath');
                $.delStorage('serviceImage');
                $.delStorage('storeInfo');
                setTimeout(function(){
                    returnapp();
                },2000);
            }else{
                YDUI.dialog.toast(result.message,'error',1000)
            }
        }

    })

}
//h5页面与app交互
function returnapp(){
    var u=navigator.userAgent;
    var isAndroid= u.indexOf('Android')>-1|| u.indexOf('Adr')>-1;   //Android终端
    var isiOS=!!u.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/);    //ios终端
    if(isAndroid){
        //alert("安卓系统");
        JSAndroid.h5Finish();
    }else if(isiOS){
        //alert("苹果系统");
        //window.webkit.messageHandlers.shareBooking.postMessage(this.data.activity.id,this.data.activity.join_company_id);
        h5Finish();
    }
}


















